package cn.iocoder.yudao.module.resource.service.creator;

import cn.iocoder.yudao.module.resource.dal.dataobject.creator.ResourceCreatorDO;
import cn.iocoder.yudao.module.resource.dal.mysql.creator.ResourceCreatorMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 创作者 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class ResourceCreatorServiceImpl implements ResourceCreatorService {

    @Resource
    private ResourceCreatorMapper creatorMapper;

    @Override
    public Long createCreator(ResourceCreatorDO creatorDO) {
        creatorMapper.insert(creatorDO);
        return creatorDO.getId();
    }

    @Override
    public void updateCreator(ResourceCreatorDO creatorDO) {
        creatorMapper.updateById(creatorDO);
    }

    @Override
    public void deleteCreator(Long id) {
        creatorMapper.deleteById(id);
    }

    @Override
    public ResourceCreatorDO getCreator(Long id) {
        return creatorMapper.selectById(id);
    }

    @Override
    public ResourceCreatorDO getCreatorByUserId(Long userId) {
        return creatorMapper.selectByUserId(userId);
    }

    @Override
    public List<ResourceCreatorDO> getCreatorList() {
        return creatorMapper.selectList();
    }

    @Override
    public void auditCreator(Long id, Integer auditStatus, String auditRemark) {
        ResourceCreatorDO updateObj = new ResourceCreatorDO();
        updateObj.setId(id);
        updateObj.setAuditStatus(auditStatus);
        updateObj.setAuditRemark(auditRemark);
        // 如果审核通过，将状态设置为正常
        if (auditStatus == 1) {
            updateObj.setStatus(1);
        }
        creatorMapper.updateById(updateObj);
    }

    @Override
    public void addPoints(Long id, Integer points) {
        ResourceCreatorDO creator = creatorMapper.selectById(id);
        if (creator != null) {
            ResourceCreatorDO updateObj = new ResourceCreatorDO();
            updateObj.setId(id);
            updateObj.setTotalPoints(creator.getTotalPoints() + points);
            updateObj.setAvailablePoints(creator.getAvailablePoints() + points);
            creatorMapper.updateById(updateObj);
        }
    }

    @Override
    public void deductPoints(Long id, Integer points) {
        ResourceCreatorDO creator = creatorMapper.selectById(id);
        if (creator != null && creator.getAvailablePoints() >= points) {
            ResourceCreatorDO updateObj = new ResourceCreatorDO();
            updateObj.setId(id);
            updateObj.setAvailablePoints(creator.getAvailablePoints() - points);
            creatorMapper.updateById(updateObj);
        }
    }

    @Override
    public void incrementMaterialCount(Long id) {
        ResourceCreatorDO creator = creatorMapper.selectById(id);
        if (creator != null) {
            ResourceCreatorDO updateObj = new ResourceCreatorDO();
            updateObj.setId(id);
            updateObj.setMaterialCount(creator.getMaterialCount() + 1);
            creatorMapper.updateById(updateObj);
        }
    }

    @Override
    public void incrementFollowerCount(Long id) {
        ResourceCreatorDO creator = creatorMapper.selectById(id);
        if (creator != null) {
            ResourceCreatorDO updateObj = new ResourceCreatorDO();
            updateObj.setId(id);
            updateObj.setFollowerCount(creator.getFollowerCount() + 1);
            creatorMapper.updateById(updateObj);
        }
    }

}
